IF EXISTS(SELECT * FROM sys.objects o WHERE o.[object_id]= OBJECT_ID('Usp_OrderSave') AND o.[type]=N'P')
BEGIN
	DROP PROC Usp_OrderSave
END
GO
CREATE PROCEDURE Usp_OrderSave(
	@OrderId UNIQUEIDENTIFIER =NULL,
	@MemLoginID NVARCHAR(50),
	@OrderNumber NVARCHAR(50),
	@Name NVARCHAR(50),
	@Email NVARCHAR(100),
	@Address NVARCHAR(250),
	@Postalcode NVARCHAR(20),
	@Tel NVARCHAR(20),
	@Mobile NVARCHAR(20),
	@ClientToSellerMsg NVARCHAR(1000),
	@DispatchModeGuid UNIQUEIDENTIFIER,
	@PaymentGuid UNIQUEIDENTIFIER,
	@OutOfStockOperate NVARCHAR(50),
	@PackGuid UNIQUEIDENTIFIER,
	@BlessCardGuid UNIQUEIDENTIFIER,
	@ActivityGuid UNIQUEIDENTIFIER,
	@InvoiceTitle NVARCHAR(50),
	@InvoiceContent NVARCHAR(50),
	@InvoiceType NVARCHAR(50),
	@RegionCode NVARCHAR(12),
	@JoinActiveType INT,
	@ActvieContent NVARCHAR(50),
	@UsedFavourTicket NVARCHAR(50),
	@LogisticsCompanyCode NVARCHAR(100),
	@CreateUser NVARCHAR(50),
	@ModifyUser NVARCHAR(50),
	@ProductPrice DECIMAL(18,2),
	@DispatchPrice DECIMAL(18,2),
	@InsurePrice DECIMAL(18,2),
	@PaymentPrice DECIMAL(18,2),
	@PackPrice DECIMAL(18,2),
	@BlessCardPrice DECIMAL(18,2),
	@AlreadPayPrice DECIMAL(18,2),
	@SurplusPrice DECIMAL(18,2),
	@UseScore INT,
	@ScorePrice DECIMAL(18,2),
	@ShouldPayPrice DECIMAL(18,2)
)
AS

DECLARE @OrderStatus INT
DECLARE @ShipmentStatus INT 
DECLARE @PaymentStatus INT 
DECLARE @CreateTime DATETIME
DECLARE @BuyType INT
DECLARE @DispatchModeName NVARCHAR(50)
DECLARE @PaymentName NVARCHAR(50)
DECLARE @PackName NVARCHAR(50)
DECLARE @BlessCardName NVARCHAR(50)
DECLARE @BlessCardContent NVARCHAR(4000)


SET @OrderStatus=0
SET @ShipmentStatus=0
SET @PaymentStatus=0
SET @BuyType=0
SET @CreateTime=GETDATE()

SELECT @DispatchModeName=Name FROM ShopNum1_DispatchMode WHERE Guid=@DispatchModeGuid
SELECT @PaymentName=NAME FROM ShopNum1_Payment WHERE Guid=@PaymentGuid
SELECT @PackName=NAME FROM ShopNum1_Pack WHERE Guid=@PackGuid
SELECT @BlessCardName=NAME,@BlessCardContent=[Description] FROM ShopNum1_BlessCard WHERE Guid=@BlessCardGuid

IF NOT EXISTS(SELECT * FROM ShopNum1_OrderInfo WHERE [Guid]=@OrderId)
BEGIN
	INSERT INTO ShopNum1_OrderInfo
	(
		Guid,MemLoginID,OrderNumber,OderStatus,ShipmentStatus,PaymentStatus,
		Name,Email,[Address],Postalcode,Tel,Mobile,
		DispatchModeGuid,DispatchModeName,PaymentGuid,PaymentName,PackGuid,PackName,
		BlessCardGuid,BlessCardName,BlessCardContent,
		BuyType,ActivityGuid,InvoiceType,InvoiceTitle,InvoiceContent,OutOfStockOperate,ClientToSellerMsg,
		CreateTime,CreateUser,ModifyUser,ModifyTime,
		RegionCode,JoinActiveType,ActvieContent,UsedFavourTicket,LogisticsCompanyCode,
		ProductPrice,DispatchPrice,InsurePrice,PaymentPrice,PackPrice,BlessCardPrice,
		AlreadPayPrice,SurplusPrice,UseScore,ScorePrice,ShouldPayPrice
	)
	VALUES
	(
		@OrderId,@MemLoginID,@OrderNumber,@OrderStatus,@ShipmentStatus,@PaymentStatus,
		@Name,@Email,@Address,@Postalcode,@Tel,@Mobile,
		@DispatchModeGuid,@DispatchModeName,@PaymentGuid,@PaymentName,@PackGuid,@PackName,
		@BlessCardGuid,@BlessCardName,@BlessCardContent,
		@BuyType,@ActivityGuid,@InvoiceType,@InvoiceTitle,@InvoiceContent,@OutOfStockOperate,@ClientToSellerMsg,
		@CreateTime,@CreateUser,@CreateUser,@CreateTime,
		@RegionCode,@JoinActiveType,@ActvieContent,@UsedFavourTicket,@LogisticsCompanyCode,
		@ProductPrice,@DispatchPrice,@InsurePrice,@PaymentPrice,@PackPrice,@BlessCardPrice,
		@AlreadPayPrice,@SurplusPrice,@UseScore,@ScorePrice,@ShouldPayPrice
	)
END
ELSE
BEGIN
	UPDATE ShopNum1_OrderInfo
	SET 
		NAME=@Name,
		Email=@Email,
		[Address]=@Address,
		Postalcode=@Postalcode,
		Tel=@Tel,
		Mobile=@Mobile,
		DispatchModeGuid=@DispatchModeGuid,
		DispatchModeName=@DispatchModeName,
		PaymentGuid=@PaymentGuid,
		PaymentName=@PaymentName,
		PackGuid=@PackGuid,
		PackName=@PackName,
		BlessCardGuid=@BlessCardGuid,
		BlessCardName=@BlessCardName,
		BlessCardContent=@BlessCardContent,
		ActivityGuid=@ActivityGuid,
		InvoiceType=@InvoiceType,
		InvoiceTitle=@InvoiceTitle,
		InvoiceContent=@InvoiceContent,
		OutOfStockOperate=@OutOfStockOperate,
		ClientToSellerMsg=@ClientToSellerMsg,
		ModifyUser=@ModifyUser,
		ModifyTime=GETDATE(),
		RegionCode=@RegionCode,
		JoinActiveType=@JoinActiveType,
		ActvieContent=@ActvieContent,
		UsedFavourTicket=@UsedFavourTicket,
		LogisticsCompanyCode=@LogisticsCompanyCode,
		ProductPrice=@ProductPrice,
		DispatchPrice=@DispatchPrice,
		InsurePrice=@InsurePrice,
		PaymentPrice=@PaymentPrice,
		PackPrice=@PackPrice,
		BlessCardPrice=@BlessCardPrice,
		AlreadPayPrice=@AlreadPayPrice,
		SurplusPrice=@SurplusPrice,
		UseScore=@UseScore,
		ScorePrice=@ScorePrice,
		ShouldPayPrice=@ShouldPayPrice
	WHERE Guid=@OrderId
END

